import os
def read_file(path):#检查文件是否存在
    if not os.path.exists(path):
        print("文件不存在")
    else:
        with open(path,"r")as f:
            for line in f :
                person = line.split("t")
                #去掉换行符
                person[-1]=person[-1].split("\n")[0]
                print(person)

                #读取表头
                if index == 0:
                    for p in person:
                        header.append(p)
                #读取数据
                else:
                    dic = dict()
                    dic[header[0]] = person[0]
                    dic[header[1]] = person[1]
                    dic[header[2]] = person[2]
                    data.append(dic)
                index +=1
        print(date)


class Movie:
    movies = []
    def __init__(self,no,mingcheng,zongpiaofang,pingfen):
        self.no = no
        self.mingcheng = mingcheng
        self.zongpiaofang = zongpiaofang
        self.pingfen = pingfen

    def do_read(self):
        path = "data/movies_data.txt"
        if not os.path.exists(path):
            print("文件不存在")
        else:
            with open(path, "r") as f:
                for line in f:
                    print(line)
                    movie_data = line.split(";")
                    one_movie  = Movie(movie_data[0],movie_data[1],movie_data[2],movie_data[3])
                    self.movies.append(one_movie)

    def do_display(self):
        for i in self.movies:
            print(i)

    def do_add_save(self):
        print("欢迎使用新增功能，请输入以下信息")
        no = input("请输入序号")
        mingcheng = input("请输入名称")
        zongpiaofang = input("请输入票房")
        pingfen = input("请输入评分")
        mov = [no,mingcheng,zongpiaofang,pingfen]
        self.movies.append(mov)

        # 写数据到txt
        # 构造写字符串
        line = no + ";" + mingcheng + ";" + zongpiaofang + ";" + pingfen + "\n"
        with open("data/movies_data.txt","a") as f:
            f.write(line)

    def do_statistic(self):
        





    def menu(self):
        print("1_读取数据到列表(输入数字1）")
        print("2_显示前10条数据和后10条数据(输入数字2）")
        print("3_汇总统计(输入数字3）")
        print("4_新增电影并保存到CSV文件(输入数字4）")

        function_num = int(input("请选择功能"))

        if function_num == 1:
            print("欢迎使用读取数据到列表")
            self.do_read()

        elif function_num == 2:
            print("欢迎使用显示前10条数据和后10条数据")
            self.do_display()

        elif function_num == 3:
            print(" 欢迎使用汇总统计")
            self.do_statistic()

        elif function_num == 4:
            print("欢迎使用新增电影并保存到CSV文件")
            self.do_add_save()
        else:
            print("请选择正确的功能")


if __name__ == '__main__':

    movie = Movie(10,"jinglinlvshe",3000,7.1)
    while 1:
        movie.menu()
